//
//  TTToolsManager.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/7.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit
import AFNetworking

enum NetType {
    case get
    case post
}
class TTToolsManager: AFHTTPSessionManager {

  static let sharedManager:TTToolsManager = {
     let tools = TTToolsManager()
        tools.responseSerializer.acceptableContentTypes?.insert("html/text")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //封装网络请求
   private func requestNetWith(type:NetType,url:String,parameter:Any?,success:@escaping (Any?,Error?)->()){
    
        if type == .get{
        self.get(url, parameters: parameter, progress: nil, success: { (_, reproject) in
            success(reproject,nil)
        }, failure: { (_, error) in
            success(nil,error)
        })
        }else{
        self.post(url, parameters: parameter, progress: nil, success: { (_, reproject) in
            success(reproject,nil)
        }, failure: { (_, error) in
             success(nil,error)
        })
        }
    }
    
    //OAuth2的access_token接口请求
    func accessToken(code:String,success:@escaping (Any?, Error?) -> ())
    {
        let url = "https://api.weibo.com/oauth2/access_token"
        let parameter:[String:Any] = [
        "client_id":964998189,
        "client_secret":"179877848cfb5ac9f064a98fae731837",
        "grant_type":"authorization_code",
        "code":code,
        "redirect_uri":"http://www.itcast.cn"
        ]
      requestNetWith(type: .post, url: url, parameter: parameter, success: success)
    
    }
    
    //用户信息接口
    func accessUerinfo(access_token:String,uid:Int64,success:@escaping (Any?, Error?) -> ())
    {
    
    let url = "https://api.weibo.com/2/users/show.json"
        let parameter:[String:Any] = [
        "access_token":access_token,
        "uid":uid
        ]
        
    requestNetWith(type: .get, url: url, parameter: parameter, success: success)
    
    }
    
    //关注用户信息接口
    
    func attentionFrome(token:String,maxid:Int64,sinceid:Int64,success:@escaping (Any?, Error?) -> ()){
    let url = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parameter:[String:Any] = [
        "access_token":token,
        "max_id":maxid,
        "since_id":sinceid
        ]
         print("\(url)?access_token=\(token)")
     requestNetWith(type: .get, url: url, parameter: parameter, success: success)
    }
    
    //发送文字接口
    func sendText(token:String,textContent:String,sucess:@escaping (Any?, Error?) -> ())
    {
     let url = "https://api.weibo.com/2/statuses/update.json"
        let paramater:[String:Any] = [
        "access_token":token,
        "status":textContent
        ]
        requestNetWith(type: .post, url: url, parameter: paramater, success: sucess)
    }
    //发送图片接口
    func sendPicture(token:String,textContent:String,pic:UIImage,sucess:@escaping (Any?, Error?) -> ()){
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        let paramater:[String:Any] = [
            "access_token":token,
            "status":textContent
        ]
        
        let data = UIImageJPEGRepresentation(pic, 0.5)
        
     post(url, parameters: paramater, constructingBodyWith: { (fromeData) in
        fromeData.appendPart(withFileData: data!, name: "pic", fileName: "aaaa", mimeType: "image/jpeg")
     }, progress: nil, success: { (_, rebose) in
        sucess(rebose,nil)
     }, failure: { (_, error) in
        sucess(nil,error)
     })
    }
    
}
